package org.beaconframework.ext.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.beaconframework.vaadin.ui.Refreshable;
import org.beaconframework.vaadin.util.VaadinContext;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.Filter;


import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.IndexedContainer;

public class IndexedLdapContainer extends IndexedContainer implements LdapContainer, Refreshable {

	private static final long serialVersionUID = -1516711430107242658L;

	LdapContextSource ldapContextSource;
	String searchBase = "";
	String searchTemplate = "";
	Object[] searchArgs = new Object[0];

	public IndexedLdapContainer() {
		super();
		
	//	LdapContextSource cs;
	//	cs.setUserDn(userDn)
	//	cs.setPassword(password)
//		cs.setBase(base);
		
	}

	
	
	public LdapContextSource getLdapContextSource() {
		LdapContextSource lcs = ldapContextSource;
		if (lcs==null) {
			lcs = VaadinContext.getApplicationContext().getBean("ldapContextSource",LdapContextSource.class);
		}
		String x = lcs.getUrls()[0];
		if (x.contains("SAMPLERSERVER")) {
			throw new IllegalStateException();
		}
		return lcs;
	}



	public void setLdapContextSource(LdapContextSource ldapContextSource) {
		this.ldapContextSource = ldapContextSource;
	
	}

	public void refresh() {
		search();
	}

	public void search() {

	
		
		
			LdapTemplate template = new LdapTemplate(getLdapContextSource());
			removeAllItems();
			DirContext ctx = getLdapContextSource().getReadOnlyContext();
			
			Filter f = new EqualsFilter("objectclass", "person");
			AttributesMapper am = new AttributesMapper() {
				
				@Override
				public Object mapFromAttributes(Attributes attributes)
						throws NamingException {
					Attributes attrs = attributes;
					NamingEnumeration<Attribute> attrEnum = (NamingEnumeration<Attribute>) attrs.getAll();
					Object tmpIdx = addItem();
					Item tmpItem = getItem(tmpIdx);
					boolean clean=true;
					while (attrEnum.hasMoreElements()) {
						Attribute attr = attrEnum.next();
						System.out.println(attr);
						
						Property prop = tmpItem.getItemProperty(attr.getID());
						if (prop!= null && prop.getType()!=null){
							
							prop.setValue(""+attr.get());
							
						}
						else {
							clean=false;
							addContainerProperty(attr.getID(), String.class, "");
						
						}
						
					}
					if (!clean) {
						removeItem(tmpIdx);
						mapFromAttributes(attributes);
					}
					return "";
				}
			};
			template.search("",f.encode(),am);
			
		
		
	}

	public Object[] getSearchArgs() {
		return searchArgs;
	}

	public void setSearchArgs(Object[] searchArgs) {
		this.searchArgs = searchArgs;
	}


	public String getSearchTemplate() {
		return searchTemplate;
	}

	public void setSearchTemplate(String searchTemplate) {
		this.searchTemplate = searchTemplate;
	}


}
